// These are meant to be typed into the REPL. You can also run
// scala -Xnojline < repl-session.scala to run them all at once.

// 部分化简操作
List(1, 7, 2, 9).reduceLeft(_ - _)
// (((1-7)-2)-9)
//res85: Int = -17

List(1, 7, 2, 9).reduceRight(_ - _)
// (1-(7-(2-9)))
//res86: Int = -13

// 折叠操作 == List(0, 1, 7, 2, 9).reduceLeft(_ - _)
List(1, 7, 2, 9).foldLeft(0)(_ - _)
// 0-1-7-2-9
//res87: Int = -19

// 可以用一下方式来表示 foldLeft
(0 /: List(1, 7, 2, 9))(_ - _)



val freq = scala.collection.mutable.Map[Char, Int]()

/**
 * freq(c) = freq.getOrElse(c, 0) + 1
 * 如果map中没有c对应的值就给0否则取出来值加1然后再复制给freq(c)
 */
for (c <- "Mississippi") freq(c) = freq.getOrElse(c, 0) + 1

(Map[Char, Int]() /: "Mississippi") {
  (m, c) => m + (c -> (m.getOrElse(c, 0) + 1))
}

// 扫描操作
/**
 * 0 = 0
 * 0+1=1
 * 0+1+2=3
 * 0+1+2+3=6
 * 0+1+2+3+4=10
 * 0+1+2+3+4+5=15
 * 0+1+2+3+4+5+6=21
 * 0+1+2+3+4+5+6+...+10=55
 */
(1 to 10).scanLeft(0)(_ + _)
//res89: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55)